package com.itheima.service.mongo.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.RandomUtil;
import com.itheima.domain.mongo.RecommendUser;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.service.mongo.RecommendUserService;
import org.apache.dubbo.config.annotation.Service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

@Service
public class RecommendUserServiceImpl implements RecommendUserService {

    @Autowired
    private MongoTemplate mongoTemplate;
    @Override
    public RecommendUser findBestUser(Long loginUserId) {
        Query query = new Query(
                Criteria.where("toUserId").is(loginUserId)
        ).with(Sort.by(Sort.Order.desc("score"))).skip(0).limit(1);//只查询第一条

        //查询数据
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);

        //构建假数据 如果大数据没有及时推送的情况下 给一条默认数据 防止报错
        if(recommendUser == null ){
            recommendUser = new RecommendUser();
            recommendUser.setUserId(1L); // 1L  1  Long类型
            recommendUser.setScore(99D);
        }

        return recommendUser;
    }

    /**
     * 查询推荐用户列表
     * @param page
     * @param pageSize
     * @param loginUserId
     * @return
     */
    @Override
    public PageBeanVo findRecommendUserList(Integer page, Integer pageSize, Long loginUserId) {
        int startIndex = (page -1)*pageSize;
        if(page == 1){ //page 页码
            startIndex=1; //从第一条开始
        }

        Query query = new Query(
                Criteria.where("toUserId").is(loginUserId)
        ).with(Sort.by(Sort.Order.desc("score"))).skip(startIndex).limit(pageSize);//跳过第一条

        //查询数据
        List<RecommendUser> recommendUserList = mongoTemplate.find(query, RecommendUser.class);
        long counts = mongoTemplate.count(query, RecommendUser.class);

        //判断是否有假数据
        if(CollectionUtil.isEmpty(recommendUserList)){
            recommendUserList = new ArrayList<>();
            for(int i = 2 ; i < 10 ; i++){
                RecommendUser recommendUser = new RecommendUser();
                recommendUser.setUserId( Long.valueOf( i )   ); //设置id
                recommendUser.setScore(RandomUtil.randomDouble(66,99));//随机分数
                recommendUserList.add(recommendUser);
            }
        }

        return new PageBeanVo(page, pageSize , counts , recommendUserList );
    }

    /**
     * 查询缘分值
     * @param loginUserId
     * @param recommendUserId
     * @return
     */
    @Override
    public RecommendUser findFateValue(Long loginUserId, Long recommendUserId) {
        Query query = new Query(
                Criteria.where("userId").is(recommendUserId).and("toUserId").is(loginUserId)
        );
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);

        if(recommendUser == null){ //防止用户点的是假数据
            recommendUser = new RecommendUser();
            recommendUser.setScore(RandomUtil.randomDouble(66,99));// 跟之前缘分值不一定一样
        }

        return recommendUser;
    }
}
